package com.mjk.manager.magicapi.core.datasource.service;


import com.mjk.manager.magicapi.core.core.config.JsonCodeConstants;
import com.mjk.manager.magicapi.core.core.model.MagicEntity;
import com.mjk.manager.magicapi.core.core.service.MagicResourceService;
import com.mjk.manager.magicapi.core.core.service.MagicResourceStorage;
import com.mjk.manager.magicapi.core.datasource.model.DataSourceInfo;
import com.mjk.manager.magicapi.core.utils.IoUtils;
import com.mjk.manager.magicapi.core.utils.JsonUtils;

import java.util.Objects;

public class DataSourceInfoMagicResourceStorage implements MagicResourceStorage<DataSourceInfo>, JsonCodeConstants {

    private MagicResourceService magicResourceService;

    @Override
    public String folder() {
        return "datasource";
    }

    @Override
    public String suffix() {
        return ".json";
    }

    @Override
    public Class<DataSourceInfo> magicClass() {
        return DataSourceInfo.class;
    }

    @Override
    public boolean requirePath() {
        return false;
    }

    @Override
    public boolean requiredScript() {
        return false;
    }

    @Override
    public boolean allowRoot() {
        return true;
    }

    @Override
    public String buildMappingKey(DataSourceInfo info) {
        return String.format("%s-%s", info.getKey(), info.getUpdateTime());
    }

    @Override
    public void validate(DataSourceInfo entity) {
        notBlank(entity.getUrl(), DS_URL_REQUIRED);
        notBlank(entity.getKey(), DS_KEY_REQUIRED);
        isTrue(IoUtils.validateFileName(entity.getKey()), DATASOURCE_KEY_INVALID);
        boolean noneMatchKey = magicResourceService.listFiles("datasource:0").stream()
                .map(it -> (DataSourceInfo) it)
                .filter(it -> !it.getId().equals(entity.getId()))
                .noneMatch(it -> Objects.equals(it.getKey(), entity.getKey()));
        isTrue(noneMatchKey, DS_KEY_CONFLICT);
    }

    @Override
    public void setMagicResourceService(MagicResourceService magicResourceService) {
        this.magicResourceService = magicResourceService;
    }

    @Override
    public DataSourceInfo read(byte[] bytes) {
        return JsonUtils.readValue(bytes, DataSourceInfo.class);
    }

    @Override
    public byte[] write(MagicEntity entity) {
        return JsonUtils.toJsonBytes(entity);
    }
}
